<template>
  <div class="login">
    <div class="container">
      <a href="/#/index"><img src="/imgs/login-logo.png" alt=""></a>
    </div>
    <div class="wrapper">
      <div class="container">
        <div class="login-form">
          <h3>
            <span class="checked">帐号登录</span><span class="sep-line">|</span><span>扫码登录</span>
          </h3>
          <div class="input">
            <input type="text" placeholder="请输入帐号" v-model="username">
          </div>
          <div class="input">
            <input type="password" placeholder="请输入密码" v-model="password">
          </div>
          <div class="btn-box">
            <a href="javascript:;" class="btn" @click="login">登录</a>
          </div>
          <div class="tips">
            <div class="sms">手机短信登录/注册</div>
            <div class="reg" @click="register">立即注册<span>|</span>忘记密码？</div>
          </div>
        </div>
      </div>
    </div>
    <nav-footer></nav-footer>
  </div>
</template>

<script>
  import NavFooter from '@/components/NavFooter'
  import { mapActions } from 'vuex'
  export default {
    name: 'login',
    components: { NavFooter },
    data() {
      return {
        username: '',
        password: '',
        userId: '',
      }
    },
    methods: {
      login() {
        let {username, password} = this;
        this.axios.post('/user/login', {username, password})
        .then(res => {
          console.log(res)
          this.$cookie.set('userId', res.id, {expires: 'Session'})
          // Vuex保存用户名
          // this.$store.dispatch('saveUserName', res.username)
          // mapActions保存用户名
          this.saveUserName(res.username)
          // 传递参数表示是从login页面跳转
          this.$router.push({
            // path: '/index',
            // query相当于get传参 地址栏拼接 明文传参
            // query: {
            //   from: 'login'
            // }
            // 注意一个是name一个是path
            name: 'index',
            params: {
              from: 'login'
            }
          })
        })
        .catch(err => {
          console.error(err); 
        })
      },
      ...mapActions(['saveUserName']),
      register() {
        let {username, password} = this;
        let time = new Date();
        this.axios.post('/user/register',{username, password, email: time+'@163.com'})
        .then(() => {
          this.$message.success('注册成功')
        })
        .catch(err => {
          console.error(err); 
        })
      }
    }
  }
</script>

<style lang='scss' scoped>
  @import '@/assets/scss/config.scss';
  @import '@/assets/scss/base.scss';
  .login{
    &>.container{
      height:113px;
      img{
        width:auto;
        height:100%;
      }
    }
    .wrapper{
      background:url('/imgs/login-bg.jpg') no-repeat center;
      .container{
        height:576px;
        .login-form{
          box-sizing: border-box;
          padding-left: 31px;
          padding-right: 31px;
          width:410px;
          height:510px;
          background-color:#ffffff;
          position:absolute;
          top:140px;
          right:250px;
          h3{
            line-height:23px;
            font-size:24px;
            text-align:center;
            margin:40px auto 49px;
            .checked{
              color:#FF6600;
            }
            .sep-line{
              margin:0 32px;
            }
          }
          .input{
            display:inline-block;
            width:348px;
            height:50px;
            border:1px solid #E5E5E5;
            margin-bottom:20px;
            input{
              width: 100%;
              height: 100%;
              border: none;
              padding-left: 20px;
              font-size: 16px;
            }
          }
          .btn{
            width:100%;
            line-height:50px;
            height: 50px;
            margin-top:10px;
            font-size:16px;
            box-sizing: border-box;
          }
          .tips{
            margin-top:14px;
            display:flex;
            justify-content:space-between;
            font-size:14px;
            cursor:pointer;
            .sms{
              color:#FF6600;
            }
            .reg{
              color:#999999;
              span{
                margin:0 7px;
              }
            }
          }
        }
      }
    }
  }
</style>
